#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# md5fdupl.py
#
# Copyright (c) 2008, Andrey Usov <uandrey@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


__version = "0.1"

import os, sys, md5flib

def md5data_sync(source, target):
    # synchronize folders based on source/target md5data files
    source_data = md5flib.load_md5data_detail(source)
    target_data = md5flib.load_md5data_detail(target)
    
    print "*** Files for copiyng from source to target"
    for md5data in set(source_data.keys()) - set(target_data.keys()):
        print source_data[md5data]
    print "*** Files for deleting from target"
    print set(target_data.keys()) - set(source_data.keys())
    
    

if __name__ == "__main__":
    from optparse import OptionParser
    
    parser = OptionParser(version="%prog, v." + __version)
    parser.add_option("-s", "--source", dest="md5data_source_file", help="source md5data file")
    parser.add_option("-t", "--target", dest="md5data_target_file", help="target md5data file")
    (options, args) = parser.parse_args()
    
    if options.md5data_source_file == None or options.md5data_target_file == None:
        # Error
        parser.error("Incorrect arguments, \n\nuse -h or --help for detail")
    else:
        # synchronize folders based on source/target md5data files
        md5data_sync(options.md5data_source_file, options.md5data_target_file)